﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
namespace IQReportes.WebService
{
    /// <summary>
    /// clase para consumir servicio rest
    /// </summary>
    public class ConsumeRestService
    {
        /// <summary>
        /// constructor
        /// </summary>
        public ConsumeRestService() { }
        /// <summary>
        /// obtener url de tipo de cambio
        /// </summary>
        /// <returns>url de tipo de cambio</returns>
        public string getUrlCurrency()
        {
            return ConfigurationManager.AppSettings["WScurrency"].ToString();
        }
        /// <summary>
        /// obtener tipo de cambio de una fecha
        /// </summary>
        /// <param name="dateCurrencyParam">fecha</param>
        /// <returns>tipo de cambio</returns>
        public Double getCurrency(DateTime dateCurrencyParam)
        {
            double errorResponse = -1.0;
            try
            {
                var stringDate = String.Format("{0:yyyy-MM-dd}", dateCurrencyParam);
                RestGeneric<Currency> client = new RestGeneric<Currency>();
                var url = string.Format(getUrlCurrency(), stringDate);
                var response = client.consume(url);
                return response.rates.PEN;

            }
            catch (Exception e)
            {
                return errorResponse;
            }
        }
    }
}